package com.gdpu.project.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.dao.CgThesisDao;
import com.gdpu.project.vo.CgProject;
import com.gdpu.project.vo.CgThesis;


public class CgThesisDaoImpl extends HibernateDaoSupport implements CgThesisDao {
	private static final Log log = LogFactory.getLog(CgThesisDaoImpl.class);
	// property constants
	public static final String TEACHER_ID = "teacherId";
	public static final String THESIS_ID = "thesisId";
	public static final String THESIS_TYPE = "thesisType";
	public static final String THESIS_TITLE = "thesisTitle";
	public static final String THESIS_EN_TITLE = "thesisEnTitle";
	public static final String KAN_WU = "kanWu";
	public static final String GRADE = "grade";
	public static final String JUAN_HAO = "juanHao";
	public static final String SCHOOL_SIGN = "schoolSign";
	public static final String PUBLISH_RANGE = "publishRange";
	public static final String QI_HAO = "qiHao";
	public static final String PAGE_RANGE = "pageRange";
	public static final String ZI_SHU = "ziShu";
	public static final String PROJECT_ORIGIN = "projectOrigin";
	public static final String XUE_KE = "xueKe";
	public static final String LAYOUT = "layout";
	public static final String YI_WEN = "yiWen";
	public static final String KE_YAN_FEN = "keYanFen";
	public static final String THESIS_ZY = "thesisZy";
	public static final String COMMENT = "comment";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#save(com.gdpu.project.vo.CgThesis)
	 */
	public void save(CgThesis transientInstance) {
		log.debug("saving CgThesis instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#delete(com.gdpu.project.vo.CgThesis)
	 */
	public void delete(CgThesis persistentInstance) {
		log.debug("deleting CgThesis instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findById(java.lang.Integer)
	 */
	public CgThesis findById(java.lang.Integer id) {
		log.debug("getting CgThesis instance with id: " + id);
		try {
			CgThesis instance = (CgThesis) getHibernateTemplate().get(
					"com.gdpu.project.vo.CgThesis", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByExample(com.gdpu.project.vo.CgThesis)
	 */
	public List<CgThesis> findByExample(CgThesis instance) {
		log.debug("finding CgThesis instance by example");
		try {
			List<CgThesis> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	public QueryResult<CgThesis> findByProperty(String propertyName, Object value,int firstindex,int maxresult) {
		log.debug("finding CgThesis instance with property: "+ propertyName+ ", value: " + value);
		QueryResult<CgThesis> queryResult=new QueryResult<CgThesis>();
		try {
			Query query=getSessionFactory().getCurrentSession().createQuery("from CgThesis as model where model."+propertyName + " = :propertyName");
			query.setParameter("propertyName", value);
			if(firstindex!=-1&&maxresult!=-1){
				query.setFirstResult(firstindex).setMaxResults(maxresult);
			}
			queryResult.setResultlist(query.list());
			query=getSessionFactory().getCurrentSession().createQuery("select count (*) from CgThesis as model where model."
					+ propertyName + "=:propertyName");
			query.setParameter("propertyName", value);
			queryResult.setTotalrecord((Long)query.uniqueResult());
			return queryResult;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByTeacherId(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByTeacherId(Object teacherId,int firstindex,int maxresult) {
		return findByProperty(TEACHER_ID, teacherId,firstindex,maxresult);
	}
	public QueryResult<CgThesis> findByTeacherId(Object teacherId) {
		return findByTeacherId(teacherId,-1,-1);	
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByThesisId(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByThesisId(Object thesisId,int firstindex,int maxresult) {
		return findByProperty(THESIS_ID, thesisId,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByThesisType(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByThesisType(Object thesisType,int firstindex,int maxresult) {
		return findByProperty(THESIS_TYPE, thesisType,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByThesisTitle(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByThesisTitle(Object thesisTitle,int firstindex,int maxresult) {
		return findByProperty(THESIS_TITLE, thesisTitle,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByThesisEnTitle(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByThesisEnTitle(Object thesisEnTitle,int firstindex,int maxresult) {
		return findByProperty(THESIS_EN_TITLE, thesisEnTitle,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByKanWu(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByKanWu(Object kanWu,int firstindex,int maxresult) {
		return findByProperty(KAN_WU, kanWu,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByGrade(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByGrade(Object grade,int firstindex,int maxresult) {
		return findByProperty(GRADE, grade,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByJuanHao(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByJuanHao(Object juanHao,int firstindex,int maxresult) {
		return findByProperty(JUAN_HAO, juanHao,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findBySchoolSign(java.lang.Object)
	 */
	public QueryResult<CgThesis> findBySchoolSign(Object schoolSign,int firstindex,int maxresult) {
		return findByProperty(SCHOOL_SIGN, schoolSign,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByPublishRange(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByPublishRange(Object publishRange,int firstindex,int maxresult) {
		return findByProperty(PUBLISH_RANGE, publishRange,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByQiHao(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByQiHao(Object qiHao,int firstindex,int maxresult) {
		return findByProperty(QI_HAO, qiHao,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByPageRange(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByPageRange(Object pageRange,int firstindex,int maxresult) {
		return findByProperty(PAGE_RANGE, pageRange,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByZiShu(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByZiShu(Object ziShu,int firstindex,int maxresult) {
		return findByProperty(ZI_SHU, ziShu,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByProjectOrigin(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByProjectOrigin(Object projectOrigin,int firstindex,int maxresult) {
		return findByProperty(PROJECT_ORIGIN, projectOrigin,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByXueKe(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByXueKe(Object xueKe,int firstindex,int maxresult) {
		return findByProperty(XUE_KE, xueKe,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByLayout(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByLayout(Object layout,int firstindex,int maxresult) {
		return findByProperty(LAYOUT, layout,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByYiWen(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByYiWen(Object yiWen,int firstindex,int maxresult) {
		return findByProperty(YI_WEN, yiWen,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByKeYanFen(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByKeYanFen(Object keYanFen,int firstindex,int maxresult) {
		return findByProperty(KE_YAN_FEN, keYanFen,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByThesisZy(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByThesisZy(Object thesisZy,int firstindex,int maxresult) {
		return findByProperty(THESIS_ZY, thesisZy,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findByComment(java.lang.Object)
	 */
	public QueryResult<CgThesis> findByComment(Object comment,int firstindex,int maxresult) {
		return findByProperty(COMMENT, comment,firstindex,maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	public QueryResult<CgThesis> findAll(int firstindex,int maxresult) {
		log.debug("finding all CgThesis instances");
		try {
			QueryResult<CgThesis> querResult=new QueryResult<CgThesis>();
			Query query=getSessionFactory().getCurrentSession().createQuery("from CgThesis cgThesis");
			if(firstindex!=-1&&maxresult!=-1){
				query.setFirstResult(firstindex).setMaxResults(maxresult);
			}
			querResult.setResultlist(query.list());
			query=getSessionFactory().getCurrentSession().createQuery("select count(*) from CgThesis cgThesis");
			querResult.setTotalrecord((Long)query.uniqueResult());
			return querResult;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#merge(com.gdpu.project.vo.CgThesis)
	 */
	public CgThesis merge(CgThesis detachedInstance) {
		log.debug("merging CgThesis instance");
		try {
			CgThesis result = (CgThesis) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#attachDirty(com.gdpu.project.vo.CgThesis)
	 */
	public void attachDirty(CgThesis instance) {
		log.debug("attaching dirty CgThesis instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.CgThesisDao#attachClean(com.gdpu.project.vo.CgThesis)
	 */
	public void attachClean(CgThesis instance) {
		log.debug("attaching clean CgThesis instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CgThesisDao getFromApplicationContext(ApplicationContext ctx) {
		return (CgThesisDao) ctx.getBean("CgThesisDAO");
	}

	public int count(String id) {
		return 0;
	}
	
	@SuppressWarnings("unchecked")
	public List<CgThesis> nfindByProperty(String propertyName, String value,Date begin,Date end) {
		log.debug("finding CgThesis instance with property: " + propertyName
				+ ", value: " + value);
		try {
			StringBuilder builder=new StringBuilder();
			String queryString = "from CgThesis as model where model."
				+ propertyName + "= ?";
			builder.append(queryString);
			if(begin!=null){
				builder.append(" and model.publishDate>=? ");
			}
			if(end!=null){
				builder.append(" and model.publishDate<=? ");
			}
			List<Object> pramList=new ArrayList<Object>();
			if(value!=null){
				pramList.add(value);
			}
			if(begin!=null){
				pramList.add(begin);
			}
			if(end!=null){
				pramList.add(end);
			}
			
			return (List<CgThesis>)getHibernateTemplate().find(builder.toString(), pramList.toArray());
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	//高级搜索的分页查询
	@SuppressWarnings("unchecked")
	public QueryResult<CgThesis> nfindByProperty(String propertyName, List value,Date begin,Date end,int firstindex,int maxresult) {
		log.debug("finding CgThesis instance with property: " + propertyName
				+ ", value: " + value.toString());
		
		try {
			QueryResult<CgThesis> queryResult=new QueryResult<CgThesis>();
			//String val[]=(String[])value.toArray();
			StringBuffer queryStr=new StringBuffer();
			queryStr.append("from CgThesis as model where model."+ propertyName + " in (:value)");
			if(begin!=null){
				queryStr.append(" and model.publishDate>=:begin");
			}
			if(end!=null){
				queryStr.append(" and model.publishDate<:end");
			}
			Query query=getSessionFactory().getCurrentSession().createQuery(queryStr.toString());
			query.setParameterList("value", value);//绑定参数
			if(begin!=null){
				query.setParameter("begin", begin);
			}
			if(end!=null){
				query.setParameter("end", end);
			}
			if(firstindex!=-1&&maxresult!=-1){
				query.setFirstResult(firstindex).setMaxResults(maxresult);
			}
			if(value.size()>0){
				queryResult.setResultlist(query.list());
			}
			
			queryStr.delete(0,queryStr.length());//清除之前的字符窜
			queryStr.append("select count(*) from CgThesis as model where model."+ propertyName + " in (:value)");
			if(begin!=null){
				queryStr.append(" and model.publishDate>=:begin");
			}
			if(end!=null){
				queryStr.append(" and model.publishDate<:end");
			}
			query=getSessionFactory().getCurrentSession().createQuery(queryStr.toString());
			query.setParameterList("value", value);//绑定参数
			if(query.getQueryString().indexOf(" and model.publishDate>=:begin")!=-1){
				query.setParameter("begin", begin);
			}
			if(query.getQueryString().indexOf(" and model.publishDate<:end")!=-1){
				query.setParameter("end", end);
			}
			if(value.size()>0){
				queryResult.setTotalrecord((Long)query.uniqueResult());
			}
			
			return queryResult;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	
}